#include <cs50.h>
#include <stdio.h>

int main(void)
{
    long number = get_long("Number: ");
    int size = 0;
    int first_digit;
    int second_digit;
    int checksum = 0;
    long remain_digit = number;

    while (remain_digit > 0)
    {
        int digit = remain_digit % 10;    // 提取最后一位数字
        remain_digit = remain_digit / 10; // 移除最后一位数字

        if (size == 0)
        {
            first_digit = digit;
        }

        if (size == 1)
        {
            second_digit = digit;
        }

        if (size % 2 == 0)
        {
            checksum += digit;
        }
        else
        {
            int doubled = digit * 2;
            checksum += doubled / 10 + doubled % 10;
        }
        size++;
    }

    if (checksum % 10 != 0)
    {
        printf("INVALID\n");
        return 0;
    }

    // 判断是哪一家的银行卡
    // AMEX
    if (size == 15 && (first_digit == 3 && (second_digit == 4 || second_digit == 7)))
    {
        printf("AMEX\n");
        return 0;
    }

    // MASTERCARD
    if (size == 16 && (first_digit == 5 && (second_digit >= 1 && second_digit <= 5)))
    {
        printf("MASTERCARD\n");
        return 0;
    }

    // VISA
    if ((size == 16 || size == 13) && first_digit == 4)
    {
        printf("VISA\n");
        return 0;
    }

    printf("INVALID\n");
    return 0;
}
